package com.example.demo.socket;



import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpServerCodec;
import io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler;
import io.netty.handler.stream.ChunkedWriteHandler;
import io.netty.handler.timeout.IdleStateHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;


/**
 * @author yj
 * @date 2021/4/6 13:39
 */
@Component

public class SocketServerChannelInitializer extends ChannelInitializer<SocketChannel> {

    private Logger logger = LoggerFactory.getLogger(SocketServerChannelInitializer.class);

    @Autowired
    private SocketServerHandler socketServerHandler;

    @Override
    protected void initChannel(SocketChannel socketChannel) {
        ChannelPipeline pipeline = socketChannel.pipeline();
        //超时
        pipeline.addLast(new IdleStateHandler(30,0,0, TimeUnit.SECONDS));
        //超时处理类
        pipeline.addLast(new SocketServerUserEventTriggeredHandler());
        pipeline.addLast(new HttpServerCodec());
        //大小
        pipeline.addLast(new HttpObjectAggregator(1024 * 1024));
        //流处理类
        pipeline.addLast(new ChunkedWriteHandler());
        //协议
        pipeline.addLast(new WebSocketServerProtocolHandler("/ws"));
        //读取类
        pipeline.addLast(socketServerHandler);
    }
}